package com.oak.interviewdemo.utils.page;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.oak.interviewdemo.common.CommonConstant;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * PageR
 *
 * @Author: guoxy
 * @Date: 2020/5/10 10:03
 * @Description:
 * @Version: 1.0
 */
@Component
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageR<D> implements Serializable {

    /**
     * 状态码
     */
    private String code;

    /**
     * 提示信息
     */
    private String msg;

    /**
     * 总页数
     */
    private int count;

    /**
     * 数据
     */
    private List<D> data;

    public PageR(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }


    public static <E> PageR<E> putData(IPage<E> page) {
        PageR<E> tableR = new PageR(PageStatus.SUCCESS.getCode(), PageStatus.SUCCESS.getMessage());
        tableR.setData(page.getRecords());
        tableR.setCount((int) page.getTotal());
        return tableR;
    }

    public static <E> PageR<E> empty() {
        PageR<E> tableR = new PageR(PageStatus.EMPTY.getCode(), PageStatus.EMPTY.getMessage());
        tableR.setData(Collections.emptyList());
        tableR.setCount(0);
        return tableR;
    }


    public static <E> PageR<E> error() {
        PageR<E> tableR = new PageR(PageStatus.ERROR.getCode(), PageStatus.ERROR.getMessage());
        tableR.setData(Collections.emptyList());
        tableR.setCount(0);
        return tableR;
    }

    public static <E> PageR<E> putCodeMsg(PageStatus pageStatus) {
        PageR<E> tableR = new PageR(pageStatus.getCode(), pageStatus.getMessage());
        tableR.setData(Collections.emptyList());
        tableR.setCount(0);
        return tableR;
    }
}